﻿using Ewell.BizComponent.Charge.UI.VM;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.Charge.UI
{
    /// <summary>
    /// Cashier.xaml 的交互逻辑
    /// </summary>
    public partial class CashierFrm : Page
    {
        public CashierVm vm { get; set; }
        public CashierFrm()
        {
            InitializeComponent();
        }

        public CashierFrm(CashierVm vm): this()
        {
            this.vm = vm;
            //初始化支付方式
            vm.payWayList?.ForEach(e =>
            {
                e.payAmt = 0;
                e.paySuccess = null;
                e.payErrorMsg = string.Empty;
            });
            //存在账户默认账户支付
            if (vm.prestorePayWay != null)
            {
                //余额充足全部支付
                if(vm.prestorePayWay.balance>= vm.chargingInfo.recAmount)
                    vm.prestorePayWay.amount = vm.chargingInfo.recAmount.Value;
                else//余额不足剩余的填到默认或者第一个支付方式
                {
                    vm.prestorePayWay.amount = vm.prestorePayWay.balance??0;
                    PayWayVm payWay = vm.otherPayWayList?.FirstOrDefault(f => f.isDefault);
                    if (payWay != null) payWay.amount = vm.chargingInfo.recAmount.Value- vm.prestorePayWay.amount;
                    else
                    {
                        payWay = vm.otherPayWayList?.FirstOrDefault();
                        payWay.amount = vm.chargingInfo.recAmount.Value- vm.prestorePayWay.amount;
                    }
                }
            }
            else//不存在账户按照设置的默认支付
            {
                PayWayVm payWay = vm.otherPayWayList?.FirstOrDefault(f => f.isDefault);
                if (payWay == null) payWay = vm.otherPayWayList?.FirstOrDefault();
                if (payWay != null) payWay.amount = vm.chargingInfo.recAmount.Value;
            }
            #region 单支付方式下拉框默认选中设置的 没有设置的默认第一个
            foreach (PayWayVm item in this.vm.otherPayWayList)
            {
                if (item.isDefault)
                    this.vm.selectPayWay = item;
            }
            if (this.vm.selectPayWay == null)
                this.vm.selectPayWay = this.vm.otherPayWayList.FirstOrDefault();
            #endregion

            this.DataContext = this.vm;
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (e.Text == ".")
            {
                int dotIndex = (sender as TextBox).Text.IndexOf('.');
                if (dotIndex >= 0)
                {
                    (sender as TextBox).CaretIndex = dotIndex + 1;
                    e.Handled = true;
                }
            }
        }


        private void Viewbox_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Hand;
        }

        private void Viewbox_MouseLeave(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = null;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ((sender as TextBox).Tag == null)
            {
                e.Handled = true;
                return;
            }
            if (string.IsNullOrWhiteSpace((sender as TextBox).Text)) (sender as TextBox).Text = "0";
            decimal? amt = this.vm.payWayList.Where(f => f.no != (sender as TextBox).Tag.ToString() && f.amount > 0)?.Sum(f => f.amount);
            decimal.TryParse((sender as TextBox).Text,out decimal value);
            if (value > this.vm.chargingInfo.recAmount- amt)//最大输入金额不能超过应收金额-已输入得金额
                (sender as TextBox).Text = (this.vm.chargingInfo.recAmount - amt)?.ToString();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (PayWayVm item in e.RemovedItems)
            {
                item.amount = 0;
            }
            //重新计算金额
            ((sender as ComboBox).SelectedItem as PayWayVm).amount = (this.vm.chargingInfo.recAmount??0) - (this.vm.prestorePayWay?.amount ?? 0);
        }
    }
}
